package com.wechat.Util;

import net.sf.json.JSONObject;

/**
 * 菜单工具类
 * @author chenpeng
 * @date 2017-02-24
 *
 */
public class MenuUtil {
	//创建菜单
	public final static String MENU_CREATE_URL="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";
	
	//查询菜单
	public final static String MENU_GET_URL="https://api.weixin.qq.com/cgi-bin/menu/get?access_token=ACCESS_TOKEN";
		
	//删除菜单
	public final static String MENU_DELETE_URL="https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN";
	/**
	 * 创建菜单
	 * @param json 菜单结构
	 * @param access_token 接口访问凭证
	 * @return true|false
	 */
	public static boolean createMenu(String json,String access_token){
		boolean result = false;
		//拼装请求地址
		String requestUrl = MENU_CREATE_URL.replace("ACCESS_TOKEN", access_token);
		//调用接口
		String respJson = CommonUtil.httpsRequest(requestUrl, "POST", json);
		//将json字符创转换成jsonobject对象
		JSONObject jsonObject = JSONObject.fromObject(respJson);
		if(null!=jsonObject){
			int errCode = jsonObject.getInt("errcode");
			//String errMsg = jsonObject.getString("errmsg");
			if(0 == errCode){
				result = true;
			}else{
				result = false;
				System.out.println("菜单创建失败："+respJson);
			}
		}
		return result;
	}
	/**
	 * 查询菜单
	 * @param accessToken接口凭证
	 * @return
	 */
	public static String getMenu(String accessToken){
		//拼装请求地址
		String requestUrl = MENU_GET_URL.replace("ACCESS_TOKEN", accessToken);
		//调用接口
		String respJson = CommonUtil.httpsRequest(requestUrl, "GET", null);
		
		return respJson;
	}
	/**
	 * 删除菜单菜单
	 * @param json 菜单结构
	 * @param access_token 接口访问凭证
	 * @return true|false
	 */
	public static boolean deleteMenu(String access_token){
		boolean result = false;
		//拼装请求地址
		String requestUrl = MENU_DELETE_URL.replace("ACCESS_TOKEN", access_token);
		//调用接口
		String respJson = CommonUtil.httpsRequest(requestUrl, "POST", null);
		//将json字符创转换成jsonobject对象
		JSONObject jsonObject = JSONObject.fromObject(respJson);
		if(null!=jsonObject){
			int errCode = jsonObject.getInt("errcode");
			String errMsg = jsonObject.getString("errmsg");
			if(0 == errCode){
				result = true;
			}else{
				result = false;
				System.out.println("菜单删除失败："+respJson);
			}
		}
		return result;
	}
}
